package com.gdzn.signal.mongo.service.impl;

import com.gdzn.common.mongo.filter.MongoQueryBuilder;
import com.gdzn.common.mongo.service.impl.MgServiceImpl;
import com.gdzn.signal.mongo.repository.MgSignalAlarmRepository;
import com.gdzn.signal.mongo.document.MgSignalAlarm;
import com.gdzn.signal.mongo.service.IMgSignalAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MgSignalAlarmServiceImpl extends MgServiceImpl<MgSignalAlarmRepository,MgSignalAlarm,String> implements IMgSignalAlarmService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MgSignalAlarmRepository mgSignalAlarmRepository;

    ExampleMatcher matcher = ExampleMatcher.matching()
//           .withMatcher("stationName",ExampleMatcher.GenericPropertyMatchers.contains())
            .withIgnorePaths("_class").withIncludeNullValues();
//            .withMatcher("devId", ExampleMatcher.GenericPropertyMatchers.contains());


    @Override
    public MgSignalAlarm findOne(MgSignalAlarm vo) {
        return this.findOne(vo,matcher);
    }
    @Override
    public List<MgSignalAlarm> findAll(MgSignalAlarm vo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "txnNo");
        return findAll(vo, sort);
    }
    @Override
    public List<MgSignalAlarm> findAll(MgSignalAlarm vo, Sort sort) {
        return this.findAll(vo, matcher,sort);
    }

    @Override
    public Page<MgSignalAlarm> findListPage(MgSignalAlarm vo, int page, int size) {
        return this.findListPage(vo, matcher,page, size);
    }

    @Override
    public void updateById(String id, Map<String, Object> updateMap) {
        // 查询条件
        Query query = Query.query(Criteria.where("_id").is(id));
        // 更新条件
        Update update = MongoQueryBuilder.buildUpdate(updateMap);
        mongoTemplate.updateFirst(query, update, MgSignalAlarm.class);

    }

    @Override
    public void updateByParam(Map<String, Object> queryMap, Map<String, Object> updateMap) {
        Criteria criteria = MongoQueryBuilder.buildCriteria(queryMap);
        Query query = Query.query(criteria);
        Update update = MongoQueryBuilder.buildUpdate(updateMap);
        mongoTemplate.updateMulti(query, update, MgSignalAlarm.class);
    }



}
